#include <QPainter>
#include <QLabel>
#include <QDebug>
#include <QScopedPointer>
#include <memory>
#include "levelscene.h"
#include "gameconfig.h"
#include "gamecontroller.h"
#include "gamepushbutton.h"
LevelScene::LevelScene(QWidget *parent) : QMainWindow(parent)
{

    this->setWindowTitle(tr("关卡选择"));
    this->setWindowIcon(QIcon(GAME_ICON));
    this->setFixedSize(GAME_WIDTH,GAME_HEIGHT);
//    this->setStyleSheet(QString("background-image: url(%1);").arg(IMG_BG_LEVELSCENE));
    //返回上一个界面的按钮
    GamePushButton *btnBack = new GamePushButton(BTN_BACK_NORMAL,BTN_BACK_PRESSED);
    btnBack->setParent(this);
    btnBack->move(GAME_WIDTH - btnBack->width(),GAME_HEIGHT - btnBack->height());
    //按钮集合
    std::vector<GamePushButton*> buttons;

    for (int i = 0; i < 20 ; i++ ) {
        int current_level_num = i + 1;
        // 使用 std::make_unique 创建 unique_ptr
        GamePushButton *btnlevel = new GamePushButton(IMG_BG_LEVEL_ICON);
        buttons.push_back(btnlevel);
        buttons[i]->setParent(this);
        buttons[i]->move(65 + i%4 * 70 , 140 + i/4 * 70);
        buttons[i]->show();
        QLabel *label = new QLabel(this);
        QFont f("宋体",15,Qt::black);
        label->setFont(f);
        label->setParent(this);
        label->move(65 + i%4 * 70 , 140 + i/4 * 70);
        label->setFixedSize(buttons[i]->width(), buttons[i]->height());
        label->setText(QString::number((current_level_num)));
        label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
        //为每一个关卡按钮创建点击事件
        connect(buttons[i], &GamePushButton::clicked, this, [=](){
            qDebug()<<"点击了一次关卡按钮！";
            GameController::getInstance()->hideLevelScene();
            GameController::getInstance()->playStartSound();
            GameController::getInstance()->showGameScene(current_level_num);

        });
    }

    //返回按钮的点击事件
    connect(btnBack, &GamePushButton::clicked, this, [=](){
        GameController::getInstance()->hideLevelScene();
        GameController::getInstance()->playBackSound();
        GameController::getInstance()->showMainScene();

    });
}

void LevelScene::paintEvent(QPaintEvent *event)
{
    QPainter levelPainter(this);
    QPixmap levelPixmap;

    // 加载背景图片
    bool result = levelPixmap.load(IMG_BG_LEVELSCENE);
    if (result) {
        levelPainter.drawPixmap(0, 0, levelPixmap);
    }

    // 加载并绘制标题图片，确保它位于背景图上方
    QPixmap titlePixmap;
    titlePixmap.load(IMG_GAME_TITLE);
    levelPainter.drawPixmap((this->width() - titlePixmap.width()) / 2, 30, titlePixmap);  // 位置调整
}
